{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "Za8-Nr5k11fh"
      },
      "source": [
        "##### Copyright 2018 The TensorFlow Authors."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "cellView": "form",
        "colab": {},
        "colab_type": "code",
        "id": "Eq10uEbw0E4l"
      },
      "outputs": [],
      "source": [
        "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "YHI3vyhv5p85"
      },
      "source": [
        "## **Introduction to Colab and Python**"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "OVi775ZJ2bsy"
      },
      "source": [
        "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/examples/blob/master/courses/udacity_intro_to_tensorflow_for_deep_learning/l01c01_introduction_to_colab_and_python.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://github.com/tensorflow/examples/blob/master/courses/udacity_intro_to_tensorflow_for_deep_learning/l01c01_introduction_to_colab_and_python.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
        "  </td>\n",
        "</table>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "F8YVA_634OFk"
      },
      "source": [
        "Welcome to this Colab where you will get a quick introduction to the Python programming language and the environment used for the course's exercises: Colab.\n",
        "\n",
        "Colab is a Python development environment that runs in the browser using Google Cloud.\n",
        "\n",
        "For example, to print \"Hello World\", just hover the mouse over [ ] and press the play button to the upper left. Or press shift-enter to execute."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "X9uIpOS2zx7k"
      },
      "outputs": [],
      "source": [
        "print(\"Hello World\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "wwJGmDrQ0EoB"
      },
      "source": [
        "## Functions, Conditionals, and Iteration\n",
        "Let's create a Python function, and call it from a loop."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "pRllo2HLfXiu"
      },
      "outputs": [],
      "source": [
        "def HelloWorldXY(x, y):\n",
        "  if (x < 10):\n",
        "    print(\"Hello World, x was < 10\")\n",
        "  elif (x < 20):\n",
        "    print(\"Hello World, x was >= 10 but < 20\")\n",
        "  else:\n",
        "    print(\"Hello World, x was >= 20\")\n",
        "  return x + y\n",
        "\n",
        "for i in range(8, 25, 5):  # i=8, 13, 18, 23 (start, stop, step)\n",
        "  print(\"--- Now running with i: {}\".format(i))\n",
        "  r = HelloWorldXY(i,i)\n",
        "  print(\"Result from HelloWorld: {}\".format(r))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "lHNmDCh0JpVP"
      },
      "outputs": [],
      "source": [
        "print(HelloWorldXY(1,2))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "kiZG7uhm8qCF"
      },
      "source": [
        "Easy, right?\n",
        "\n",
        "If you want a loop starting at 0 to 2 (exclusive) you could do any of the following"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "m8YQN1H41L-Y"
      },
      "outputs": [],
      "source": [
        "print(\"Iterate over the items. `range(2)` is like a list [0,1].\")\n",
        "for i in range(2):\n",
        "  print(i)\n",
        "\n",
        "print(\"Iterate over an actual list.\")\n",
        "for i in [0,1]:\n",
        "  print(i)\n",
        "\n",
        "print(\"While works\")\n",
        "i = 0\n",
        "while i < 2:\n",
        "  print(i)\n",
        "  i += 1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "vIgmFZq4zszl"
      },
      "outputs": [],
      "source": [
        "print(\"Python supports standard key words like continue and break\")\n",
        "while True:\n",
        "  print(\"Entered while\")\n",
        "  break"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "5QyOUhFw1OUX"
      },
      "source": [
        "## Numpy and lists\n",
        "Python has lists built into the language.\n",
        "However, we will use a library called numpy for this.\n",
        "Numpy gives you lots of support functions that are useful when doing Machine Learning.\n",
        "\n",
        "Here, you will also see an import statement. This statement makes the entire numpy package available and we can access those symbols using the abbreviated 'np' syntax."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "4Dxk4q-jzEy4"
      },
      "outputs": [],
      "source": [
        "import numpy as np  # Make numpy available using np.\n",
        "\n",
        "# Create a numpy array, and append an element\n",
        "a = np.array([\"Hello\", \"World\"])\n",
        "a = np.append(a, \"!\")\n",
        "print(\"Current array: {}\".format(a))\n",
        "print(\"Printing each element\")\n",
        "for i in a:\n",
        "  print(i)\n",
        "\n",
        "print(\"\\nPrinting each element and their index\")\n",
        "for i,e in enumerate(a):\n",
        "  print(\"Index: {}, was: {}\".format(i, e))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "RTa8_9G3LV03"
      },
      "outputs": [],
      "source": [
        "print(\"\\nShowing some basic math on arrays\")\n",
        "b = np.array([0,1,4,3,2])\n",
        "print(\"Max: {}\".format(np.max(b)))\n",
        "print(\"Average: {}\".format(np.average(b)))\n",
        "print(\"Max index: {}\".format(np.argmax(b)))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "9YaGj5n4LW7P"
      },
      "outputs": [],
      "source": [
        "print(\"\\nYou can print the type of anything\")\n",
        "print(\"Type of b: {}, type of b[0]: {}\".format(type(b), type(b[0])))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "V6ilVhi9LXn_"
      },
      "outputs": [],
      "source": [
        "print(\"\\nUse numpy to create a [3,3] dimension array with random number\")\n",
        "c = np.random.rand(3, 3)\n",
        "print(c)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "W_Q-DkFCLYGA"
      },
      "outputs": [],
      "source": [
        "print(\"\\nYou can print the dimensions of arrays\")\n",
        "print(\"Shape of a: {}\".format(a.shape))\n",
        "print(\"Shape of b: {}\".format(b.shape))\n",
        "print(\"Shape of c: {}\".format(c.shape))\n",
        "print(\"...Observe, Python uses both [0,1,2] and (0,1,2) to specify lists\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "c-Jk4dG91dvD"
      },
      "source": [
        "## Colab Specifics"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "G0cGd8sHEmKi"
      },
      "source": [
        "Colab is a virtual machine you can access directly. To run commands at the VM's terminal, prefix the line with an exclamation point (!).\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "cLkfhyzq0W2y"
      },
      "outputs": [],
      "source": [
        "print(\"\\nDoing $ls on filesystem\")\n",
        "!ls -l\n",
        "!pwd"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "gR2WTN1cOZ1n"
      },
      "outputs": [],
      "source": [
        "print(\"Install numpy\")  # Just for test, numpy is actually preinstalled in all Colab instances\n",
        "!pip install numpy"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "QuWRpQdatAIU"
      },
      "source": [
        "**Exercise**\n",
        "\n",
        "Create a code cell underneath this text cell and add code to:\n",
        "\n",
        "\n",
        "*   List the path of the current directory (pwd)\n",
        "* Go to / (cd) and list the content (ls -l)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "xU-cJbMCR61P"
      },
      "outputs": [],
      "source": [
        "!pwd\n",
        "!cd /\n",
        "!ls -l\n",
        "print(\"Hello\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "7b5jv0ouFREV"
      },
      "source": [
        "All usage of Colab in this course is completely free or charge. Even GPU usage is provided free of charge for some hours of usage every day.\n",
        "\n",
        "**Using GPUs**\n",
        "* Many of the exercises in the course executes more quickly by using GPU runtime: Runtime | Change runtime type | Hardware accelerator | GPU\n",
        "\n",
        "**Some final words on Colab**\n",
        "*   You execute each cell in order, you can edit & re-execute cells if you want\n",
        "*   Sometimes, this could have unintended consequences. For example, if you add a dimension to an array and execute the cell multiple times, then the cells after may not work. If you encounter problem reset your environment:\n",
        "  *   Runtime -> Restart runtime... Resets your Python shell\n",
        "  *   Runtime -> Restart all runtimes... Will reset the Colab image, and get you back to a 100% clean environment\n",
        "* You can also clear the output in the Colab by doing: Edit -> Clear all outputs\n",
        "* Colabs in this course are loaded from GitHub. Save to your Google Drive if you want a copy with your code/output: File -> Save a copy in Drive...\n",
        "\n",
        "**Learn More**\n",
        "*   Check out [this](https://www.youtube.com/watch?v=inN8seMm7UI&list=PLQY2H8rRoyvwLbzbnKJ59NkZvQAW9wLbx&index=3) episode of #CodingTensorFlow, and don't forget to subscribe to the YouTube channel ;)\n"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "collapsed_sections": [],
      "name": "l01c01_introduction_to_colab_and_python.ipynb",
      "private_outputs": true,
      "provenance": [],
      "toc_visible": true,
      "version": "0.3.2"
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
